package org.exam.system.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.exam.system.annotation.PreAuthorize;
import org.exam.system.domain.Permission;
import org.exam.system.service.IPermissionService;
import org.exam.system.query.PermissionQuery;
import org.exam.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@Api(tags = "用户提示")
public class PermissionController {
    @Autowired
    private IPermissionService iPermissionService;

    @ApiOperation(value = "查询单个接口")
    @PutMapping
    @PreAuthorize(name = "保存权限",sn = "permission:save",descs = "添加和修改权限")
    public JsonResult add(@RequestBody Permission permission) {
        try {
            if (permission.getId() == null) {
                iPermissionService.add(permission);
                return JsonResult.success();
            } else {
                iPermissionService.update(permission);
                return JsonResult.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();

        }
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable Long id) {
        try {
            iPermissionService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }
    @GetMapping
    public List<Permission> getAll() {
        return iPermissionService.getAll();
    }

    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个权限",sn = "permission:getOne",descs = "根据Id查询权限")
    public Permission get(@PathVariable("id") Long id) {
        return iPermissionService.get(id);
    }


    @PostMapping
    public PageInfo<Permission> queryPage(@RequestBody PermissionQuery permissionQuery) {
        return iPermissionService.queryPage(permissionQuery);
    }


}
